package com.luzhikun.upload3;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
/*
 * @Author liu-miss
 * @Description用于处理文件上传
 * @Date 15:41 2021/5/6
 * @Param
 * @return
 **/
@WebServlet(name = "UploadServlet", value = "/uploadServlet")
// 表名当前Servlet可以处理Multipart请求
@MultipartConfig
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 获取服务端保存上传文件的路径
        String realPath = this.getServletContext().getRealPath("/images");
        System.out.println(realPath);

        // 从请求中获取MultiPart请求中的文件 数据
        Part fileP = request.getPart("fileP");
        // 获取指定的头部属性
        String header = fileP.getHeader("Content-Disposition");
        System.out.println("header==="+header);
        // 解析出文件名
        int i = header.lastIndexOf("=");
        String substring = header.substring(i + 2, header.length() - 1);
        System.out.println(substring);
        // 完成文件上传
        fileP.write(realPath+substring);

    }
}
